﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using DeferredWaterDemo;

namespace Demo
{
    public class EngineResource
    {
        private static EffectPool shaderPool;
        public static bool LoadShader(string filename, out Effect effect)
        {
            CompiledEffect cs;
            try
            {
                if (filename == null)
                    throw new Exception();
                if (filename.Length < 3)
                    throw new Exception();

                cs = Effect.CompileEffectFromFile(filename,
                        null, null, CompilerOptions.None, TargetPlatform.Windows);
                
                if (shaderPool == null)
                    shaderPool = new EffectPool();

                effect = new Effect(Render.GraphicsDevice,
                cs.GetEffectCode(), CompilerOptions.None,  shaderPool);
                return true;
            }
            catch (Exception e)
            {
                   System.Windows.Forms.MessageBox.Show(cs.ErrorsAndWarnings);
                
                effect = null;
                return false;
            }
        }

        public static bool LoadTexture(string filename, out Texture texture)
        {
            try
            {
                if (filename == null)
                    throw new Exception();
                if (filename.Length < 3)
                    throw new Exception();
                
                texture = Texture.FromFile(Render.GraphicsDevice, filename);
                return true;
            }
            catch (Exception e)
            {
                texture = null;
                return false;
            }
        }
    }
}
